<template>
  <div class="app">
    <div class="box">
      <div class="circle-box">
        <div class="circle"></div>
        <div class="circle"></div>
      </div>
      <div class="bg-filter">
        <ul class="bubble-bgwall">
          <li></li>
          <li></li>
          <li></li>
          <li></li>
          <li></li>
          <li></li>
          <li></li>
          <li></li>
          <li></li>
          <li></li>
        </ul>
        <div class="login-wrap">
          <el-row>
            <el-col :xs="2" :sm="4" :md="6" :lg="8" :xl="9"
              ><div class="grid-content"></div
            ></el-col>
            <el-col :xs="20" :sm="16" :md="12" :lg="8" :xl="6"
              ><el-form
                ref="form"
                :model="registerForm"
                :rules="rules"
                label-width="12px"
                label-position="right"
                class="form login-container"
              >
                <el-form-item class="login-title">
                  <div>
                    <h1>欢迎注册</h1>
                    <!-- <img src="../../assets/img/logo.jpg" /> -->
                  </div>
                </el-form-item>

                <el-form-item prop="email" required label="*">
                  <el-input
                    placeholder="请输入邮箱"
                    type="text"
                    v-model="registerForm.email"
                    autocomplete="off"
                    clearable
                    @keyup.enter.native="register('')"
                  >
                    <i slot="prefix" class="el-icon-message"> </i
                  ></el-input>
                </el-form-item>
                <el-form-item prop="nickname" required label="*">
                  <el-input
                    placeholder="请输入昵称"
                    type="text"
                    v-model="registerForm.nickname"
                    autocomplete="off"
                    clearable
                    @keyup.enter.native="register('')"
                  >
                    <i slot="prefix" class="el-icon-user"> </i
                  ></el-input>
                </el-form-item>
                <el-form-item prop="password" required label="*">
                  <el-input
                    placeholder="请输入密码"
                    type="password"
                    v-model="registerForm.password"
                    show-password
                    autocomplete="off"
                    clearable
                    @keyup.enter.native="register('')"
                  >
                    <i slot="prefix" class="el-icon-lock"></i>
                  </el-input>
                </el-form-item>
                <el-form-item
                  prop="code"
                  style="width: 300px"
                  required
                  label="*"
                >
                  <el-input
                    style="width: 200px"
                    type="text"
                    v-model="registerForm.code"
                    placeholder="请输入验证码"
                    autocomplete="off"
                    @keyup.enter.native="confirm"
                  >
                  </el-input>
                </el-form-item>
                <el-form-item>
                  <el-button
                    @click="send"
                    :disabled="getCodeBtnDisable"
                    :class="{ 'disabled-style': getCodeBtnDisable }"
                    style="cursor: pointer"
                    class="code"
                    >{{ codeBtnWord }}</el-button
                  >
                </el-form-item>
                <el-form-item
                  ><el-button
                    @click="register()"
                    :loading="loginLoading"
                    round
                    style="width: 100%"
                    >注册</el-button
                  >
                </el-form-item>
                <el-form-item>
                  <el-link
                    type="primary"
                    @click="login"
                    style="float: right; margin-top: 10%"
                    >立即登录></el-link
                  >
                </el-form-item>
              </el-form></el-col
            >
            <el-col :xs="2" :sm="4" :md="6" :lg="8" :xl="9"
              ><div class="grid-content"></div
            ></el-col>
          </el-row>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { sendCode } from "../../api/sendCode";
import { register } from "../../api/register";

export default {
  data() {
    return {
      codeBtnWord: "获取验证码", // 获取验证码按钮文字
      // 登录表单参数
      waitTime: 61, // 获取验证码按钮失效时间
      registerForm: {
        email: "",
        nickname: "",
        password: "",
        code: "",
      },
      // 登录按钮加载控制
      loginLoading: false,
      //表单的规则
      rules: {
        email: [{ required: true, message: "请输入邮箱", trigger: "blur" }],
        nickname: [{ required: true, message: "请输入昵称", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        code: [{ required: true, message: "请输入验证码", trigger: "blur" }],
      },
    };
  },
  computed: {
    // 控制获取验证码按钮是否可点击
    getCodeBtnDisable: {
      get() {
        if (this.waitTime == 61) {
          if (this.registerForm.email) {
            return false;
          }
          return true;
        }
        return true;
      },
      // 注意：因为计算属性本身没有set方法，不支持在方法中进行修改，而下面我要进行这个操作，所以需要手动添加
      set() {},
    },
  },
  methods: {
    //跳转登录页面
    login() {
      this.$router.push("/login");
    },
    // 发送验证码
    send() {
      this.code_disabled = true;
      sendCode(this.registerForm.email)
        .then((res) => {
          console.log(res);
          if (res.data.code == 20000) {
            // this.$message.success("验证码发送成功!");
            this.$notify({
              title: "成功",
              message: "验证码在五分钟内有效",
              type: "success",
            });
          } else {
            this.$message.error("验证码发送失败，请稍后重试！");
          }
        })
        .catch((error) => {
          this.confirm_disabled = false;
          this.$message.error(error, "验证码发送失败，请稍后重试！");
        });
      // 因为下面用到了定时器，需要保存this指向
      let that = this;
      that.waitTime--;
      that.getCodeBtnDisable = true;
      this.codeBtnWord = `${this.waitTime}s 后重新获取`;
      let timer = setInterval(function () {
        if (that.waitTime > 1) {
          that.waitTime--;
          that.codeBtnWord = `${that.waitTime}s 后重新获取`;
        } else {
          clearInterval(timer);
          that.codeBtnWord = "获取验证码";
          that.getCodeBtnDisable = false;
          that.waitTime = 61;
        }
      }, 1000);
    },
    /**
     * 注册事件
     */
    register() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.loginLoading = true;
          console.log(this.registerForm);
          register(this.registerForm).then((res) => {
            console.log(res);
            if (res.data.code === 20000) {
              this.$message.success("注册成功");
              this.$router.push("/login");
            } else {
              this.$message.error(res.data.message);
              this.$router.push("/register");
            }
          });
        }
      });
    },
  },
};
</script>

<style scoped>
.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}
.center {
  text-align: center;
}
.grid-content {
  /* border-radius: 4px; */
  min-height: 36px;
}
.flex-warp {
  flex-wrap: wrap;
}
.login-title {
  text-align: center;
  /* border: 1px solid black; */
  margin-top: 5%;
  margin-bottom: 5%;
}
.login-title img {
  height: 32px;
  vertical-align: middle;
}
.login-title h1 {
  font-size: 20px;
  font-weight: 600;
  /* vertical-align: middle; */
  display: inline-block;
  line-height: 32px;
}
.login-wrap {
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  padding-top: 5%;

  /* background-color: #112346; */
  background-repeat: no-repeat;
  background-position: center right;
  background-size: 100%;
}
.login-container {
  border-radius: 10px;
  margin: 0px auto;
  width: 350px;
  padding: 0px 35px 0px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  text-align: left;
  box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.1);
}
.app {
  width: 100%;
  height: calc(100vh - 57px);
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  background-image: linear-gradient(270deg, #f1e1ff, #b8ccff);
}
.box {
  width: 100%;
  height: 100%;
  position: relative;
}
.circle-box {
  width: 100%;
  height: 100%;
  border-radius: 10px;
  position: absolute;
  overflow: hidden;
}
.circle:first-child {
  width: 200px;
  height: 200px;
  border-radius: 50%;
  border: 30px solid #afe7ff;
  box-sizing: border-box;
  position: absolute;
  top: -60px;
  left: -70px;
  animation: move-y 3.5s linear infinite;
}

.circle:last-child {
  width: 150px;
  height: 160px;
  border-radius: 50%;
  background: linear-gradient(136deg, #e6ecff 0%, #a4b4fc 100%);
  box-sizing: border-box;
  position: absolute;
  bottom: -30px;
  right: -30px;
  animation: move-y 5s ease-in-out infinite;
}
.bg-filter {
  width: 100%;
  height: 100%;
  background: rgba(255, 255, 255, 0.05);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(8px);
  border-radius: 10px;
  box-sizing: border-box;
  position: absolute;
}
@keyframes move-y {
  0% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-16px);
  }
  100% {
    transform: translateY(0);
  }
}
.bubble-bgwall {
  overflow: hidden;
  position: fixed;
  margin: 0 auto;
  width: 100%;
  height: 100%;
  padding: 0;
}

.bubble-bgwall li {
  display: flex;
  position: absolute;
  bottom: -200px;
  justify-content: center;
  align-items: center;
  border-radius: 10px;
  width: 50px;
  height: 50px;
  background-color: rgba(255, 255, 255, 0.15);
  color: #ccc;
  animation: bubble 15s infinite;
  z-index: 999;
}

.bubble-bgwall li:nth-child(1) {
  left: 10%;
}

.bubble-bgwall li:nth-child(2) {
  left: 20%;
  width: 90px;
  height: 90px;
  animation-duration: 7s;
  animation-delay: 2s;
}

.bubble-bgwall li:nth-child(3) {
  left: 25%;
  animation-delay: 4s;
}

.bubble-bgwall li:nth-child(4) {
  left: 40%;
  width: 60px;
  height: 60px;
  background-color: rgba(255, 255, 255, 0.3);
  animation-duration: 8s;
}

.bubble-bgwall li:nth-child(5) {
  left: 70%;
}

.bubble-bgwall li:nth-child(6) {
  left: 80%;
  width: 120px;
  height: 120px;
  background-color: rgba(255, 255, 255, 0.2);
  animation-delay: 3s;
}

.bubble-bgwall li:nth-child(7) {
  left: 32%;
  width: 160px;
  height: 160px;
  animation-delay: 2s;
}

.bubble-bgwall li:nth-child(8) {
  left: 55%;
  width: 40px;
  height: 40px;
  font-size: 12px;
  animation-duration: 15s;
  animation-delay: 4s;
}

.bubble-bgwall li:nth-child(9) {
  left: 25%;
  width: 40px;
  height: 40px;
  background-color: rgba(255, 255, 255, 0.3);
  font-size: 12px;
  animation-duration: 12s;
  animation-delay: 2s;
}

.bubble-bgwall li:nth-child(10) {
  left: 85%;
  width: 40px;
  height: 40px;
  animation-delay: 5s;
}
/*验证码样式*/
.code {
  float: right;
  width: 122px;
  height: 35px;
  margin-right: 0;
  margin-top: -60px;
  background-color: rgb(250, 250, 250);
  border-radius: 9px;
  text-align: center;
  line-height: 29px;
  padding: 3px;
}

@keyframes bubble {
  0% {
    opacity: 0.5;
    transform: translateY(0) rotate(45deg);
  }

  25% {
    opacity: 0.75;
    transform: translateY(-400px) rotate(90deg);
  }

  50% {
    opacity: 1;
    transform: translateY(-600px) rotate(135deg);
  }

  100% {
    opacity: 0;
    transform: translateY(-1000px) rotate(180deg);
  }
}
</style>
